package crp.infra.storage;

import com.qwlabs.storage.minio.MinioConfig;
import com.qwlabs.storage.services.StorageEngine;
import io.quarkus.arc.lookup.LookupIfProperty;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import java.util.Optional;

import static com.qwlabs.storage.minio.MinioStorageHelper.createMinioClient;


@Dependent
public class DefaultStorageBootstrap {

    protected static final String ENABLED = "app.storage.enabled";
    private static final String URL = "app.storage.url";
    private static final String ACCESS_KEY = "app.storage.access-key";
    private static final String SECRET_KEY = "app.storage.secret-key";

    @Produces
    @ApplicationScoped
    @LookupIfProperty(name = ENABLED, stringValue = "true")
    public StorageEngine produceStorageEngine(@ConfigProperty(name = URL) Optional<String> url,
                                              @ConfigProperty(name = ACCESS_KEY) Optional<String> accessKey,
                                              @ConfigProperty(name = SECRET_KEY) Optional<String> secretKey) {
        var client = createMinioClient(MinioConfig.builder()
                .url(url.orElse(null))
                .accessKey(accessKey.orElse(null))
                .secretKey(secretKey.orElse(null))
                .build());
        return new DefaultStorageEngine(client);
    }
}
